//
//  FYVisitorLoginView.swift
//  dugeweibo
//
//  Created by pelesys on 16/8/4.
//  Copyright © 2016年 杜方宜. All rights reserved.
//

import UIKit
//可选的协议方法 是OC的关键字 在swift中声明(兼容OC)加@objc
//可选的协议方法 不能用"!"(表示遵守协议的对象一定实现方法) 这样不安全 应该加问号
//"?"判断代理为不为nil "?"判断代理是否响应方法
@objc protocol FYVisitorLoginViewDelagate:NSObjectProtocol {
    //协议方法
    //登录
    func visitorWillLogin()
    //注册
    @objc optional func visitorWillRegistor()
}
class FYVisitorLoginView: UIView {
    
    //声明代理属性 代理类型为协议名
    //Swift属性默认是强引用  需要添加weak
    weak var visitorDelegate:FYVisitorLoginViewDelagate?
    
    
    //MARK:按钮点击事件
    @objc  func loginBtnClick(){
       //代理调用协议方法
        visitorDelegate?.visitorWillLogin()
        
    }
//第一个"?"判断代理为不为nil 第二个"?"判断代理是否响应方法
    @objc  func registBtnClick(){
        visitorDelegate?.visitorWillRegistor?()
    }

//先思路 后代码
//1.分析访客视图 都有登录注册按钮
//2.都有提示方案
//3.固定显示的图标
//4.首页有小图标旋转的动画
//先构架视图元素
//slider解决图片拉伸问题
//视图不能跳转 要将事件传递到控制器才行/代理/闭包/addtarget
   

    
    //MARK:设置页面信息
    func setUIInfo(imageName:String?,title:String){
        iconView.isHidden = false
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.isHidden = true
        }else{
            //让circleView动起来
            startAnimation()
        }
       
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //当动画结束 或者视图处于非活跃状态,动画与不移除图层
        anim.isRemovedOnCompletion = false
        circleView.layer.add(anim, forKey: nil)
    }
    
   //重写构造方法 外界调用时候生成相应元素
    init(){
        //设置CGRectZero不影响赋值
        super.init(frame: CGRect.zero)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   //MARK:设置访客视图
    private func setupUI(){
       backgroundColor = UIColor(white: 0.93, alpha: 1)
        addSubview(circleView)
        
        addSubview(iconView)
       
        addSubview(backView)
         addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registBtn)
        
    //设置frame布局失效 NSLayoutConstraint约束才可以用
           for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
    //MARK:设置布局
    //VFL语言设置语言 
         //"view1.attr1 = view2.attr2 * multiplier + constant"
    //圆圈的约束
    //布局的约束 尽量相对一个控件
    //添加约束addConstraint
    //水平方向约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
   //竖直约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
    

     //大投标的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: .equal, toItem: circleView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: .equal, toItem: circleView, attribute: .centerY, multiplier: 1.0, constant: 0))
   //设置提示文案的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: .equal, toItem: circleView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutConstraint.Attribute.top, relatedBy: .equal, toItem: circleView, attribute: .bottom, multiplier: 1.0, constant: 16))
    //设置宽度约束
    // If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
    //设置宽度没有参照视图
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50
        ))
        
    //设置登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutConstraint.Attribute.left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutConstraint.Attribute.top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 35))
        //设置提示文案的约束
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutConstraint.Attribute.right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutConstraint.Attribute.top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 35))
        //设置背景视图的约束
        //位移枚举||  用数组[]放位移枚举
        //对视图宽高约束的数值 [String : NSNumber]可选项科可以传nil
        //views字典

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[backView]-(-45)-[registBtn]", options: [], metrics: nil, views: ["backView":backView,"registBtn":registBtn]))
        
        
        //添加登录注册点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        registBtn.addTarget(self, action: #selector(registBtnClick), for: .touchUpInside)
    }
     //MARK:懒加载所有的控件
   //首页控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
     private lazy var backView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜"
        l.textAlignment = NSTextAlignment.center
        l.font = UIFont.systemFont(ofSize: 14)
        l.textColor = UIColor.lightGray
        l.numberOfLines = 0
//        l.sizeToFit()
        return l
    }()
    //登录注册
    private lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        btn.setTitleColor(UIColor.lightGray, for: .normal)
        return btn
    }()
    //注册注册
    private lazy var registBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        return btn
    }()
}
